<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns:f="livcos.org/ns/xslt/function"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns="http://www.w3.org/1999/xhtml"
  xmlns:v="livcos.org/ns/view"
  exclude-result-prefixes="doc pm f xs v">
  
	<xsl:import href="/livcos.org/common/base.xsl"/>
	
  <xsl:template match="pm:usecase_overview">
    <table border="1" cellspacing="0" cellpadding="2" class="overview">
    	<v:style ref="{resolve-uri('styles.css')}"/>
    	<thead>
    		<tr>
    			<th>Use Case</th>
    			<th>Description</th>
    			<th>Actor(s)</th>
    		</tr>
    	</thead>
    	<tbody>
      	<xsl:apply-templates select="//pm:usecase" mode="overview"/>
    	</tbody>
    </table>
  </xsl:template>
  
	<xsl:template match="pm:usecase" mode="overview">
		<tr id="{@id}~1">
			<td>
				<a href="#{@id}"><xsl:value-of select="pm:title" /></a>
			</td>
			<td>
				<xsl:apply-templates select="pm:summary" />
			</td>
			<td>
				<xsl:apply-templates select="pm:sequence/pm:step" mode="actors" />
				<xsl:if test="not(pm:sequence/pm:step)">&#160;</xsl:if>
			</td>
		</tr>
	</xsl:template>
	
	<xsl:template match="pm:step" mode="actors">
    <xsl:if test="not(preceding-sibling::pm:step[@actor = current()/@actor])">
      <xsl:if test="position() > 1">, </xsl:if>
      <a href="{@actor}"><xsl:value-of select="//pm:actor[@id = substring(current()/@actor, 2)]/@name" /></a>
    </xsl:if>
  </xsl:template>

  <xsl:template match="pm:usecase">
    <v:chapter id="{@id}">
    	<v:title><xsl:value-of select="pm:title" /></v:title>
    	<v:style ref="{resolve-uri('styles.css')}"/>
    	<p><xsl:apply-templates select="pm:summary" /></p>
      <xsl:apply-templates select="pm:precondition" />
      <xsl:if test="pm:sequence/pm:step">
        <table border="0" cellspacing="1" cellpadding="2" class="sequence">
          <xsl:apply-templates select="pm:sequence/pm:step" />
        </table>
      </xsl:if>
      <xsl:apply-templates select="pm:postcondition" />
      <xsl:if test="pm:uc-test">
        <p>Manual Tests</p>
        <table cellspacing="0" class="test_list">
          <thead>
            <tr>
              <th>Time</th>
              <th>Tester</th>
              <th>Result</th>
            </tr>
          </thead>
          <tbody>
            <xsl:apply-templates select="pm:uc-test"/>
          </tbody>
        </table>
      </xsl:if>
    </v:chapter>
  </xsl:template>

	<xsl:template match="pm:summary">
		<xsl:apply-templates select="node()"/>
	</xsl:template>
	
	<xsl:template match="pm:actor">
		<v:chapter id="{@id}">
			<v:title><xsl:value-of select="@name"/></v:title>
      <p class="block"><xsl:value-of select="." /></p>
      <!-- xsl:variable name="usecases" select="//usecase[step/@actor = current()/@id]"/>
      <xsl:choose>
        <xsl:when test="count($usecases) = count(//usecase)">
          <p>Ist an allen Use Cases beteiligt.</p>
        </xsl:when>
        <xsl:otherwise>
          <p>Ist an den Use Cases <xsl:apply-templates select="$usecases" mode="array"/> beteiligt.</p>
        </xsl:otherwise>
      </xsl:choose-->
    </v:chapter>
  </xsl:template>

  <xsl:template match="pm:usecase" mode="array">
    <xsl:choose>
      <xsl:when test="(position() = last()) and (position() > 1)"> und </xsl:when>
      <xsl:when test="position() > 1">, </xsl:when>
    </xsl:choose>
    <a href="#{@id}"><xsl:value-of select="pm:title" /></a>
  </xsl:template>

  <xsl:template match="pm:step">
    <tr id="{@id}">
      <td align="right"><xsl:value-of select="count(preceding-sibling::pm:step)+1"/>.</td>
      <td>&#160;</td>
      <td><xsl:value-of select="//pm:actor[@id = substring(current()/@actor, 2)]/@name"/></td>
      <td>&#160;</td>
      <td><xsl:value-of select="pm:description"/></td>
    </tr>
  </xsl:template>

  <xsl:template match="pm:precondition">
    <p>Precondition: <xsl:apply-templates select="pm:val | pm:operand"/></p>
  </xsl:template>

  <xsl:template match="pm:postcondition">
    <p>Postcondition: <xsl:apply-templates select="pm:val | pm:operand"/></p>
  </xsl:template>

  <xsl:template match="pm:val">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="@ref = '#'">
        <span id="{@id}">
          <xsl:value-of select="."/>
          <xsl:if test="normalize-space(.) = ''"><i>--condition--</i></xsl:if>
        </span>
      </xsl:when>
      <xsl:when test="@ref">
        <xsl:variable name="ucUri" select="f:cosmos-uri(@ref, .)"/>
        <xsl:variable name="uc" select="//*[@id = $ucUri/id]" />
        <a id="{@id}" href="{@ref}">
          <xsl:value-of select="$uc/pm:title"/>
        	<xsl:if test="normalize-space($uc/pm:title) = ''"><i>--<xsl:value-of select="@ref"/>--</i></xsl:if>
        </a>
      </xsl:when>
      <xsl:otherwise>
        <span id="{@id}"><xsl:value-of select="."/></span>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="operand">
  </xsl:template>
  
  <xsl:template match="pm:operand" mode="operand">
    <xsl:text> </xsl:text>
    <span class="math"><xsl:value-of select="@type"/></span>
    <xsl:text> </xsl:text>
  </xsl:template>

  <xsl:template match="pm:operand">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
		<span id="{@id}">
			<xsl:if test="count(../*) > 1">(</xsl:if>
			<xsl:apply-templates select="pm:val | pm:operand"/>
			<xsl:if test="count(../*) > 1">)</xsl:if>
		</span>
  </xsl:template>

  <xsl:template match="pm:uc-test">
    <tr id="{@id}">
      <td><xsl:value-of select="format-dateTime(xs:dateTime(@time), '[D01].[M01].[Y] [H01]:[m01]')"/></td>
      <td align="center"><xsl:value-of select="@actor"/></td>
      <td><xsl:value-of select="if (xs:boolean(@passed)) then 'passed' else 'failed'"/></td>
    </tr>
  </xsl:template>
	
	<!-- so far for the /livcos.org/LivCos/data/blog/UseCaseExample only... -->

	<xsl:template match="pm:activities">
		<table border="1" cellspacing="0" cellpadding="2" class="overview">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<thead>
				<tr>
					<th>Priority</th>
					<th>Use Case</th>
					<th>Resource</th>
					<th>Start</th>
					<th>End</th>
				</tr>
			</thead>
			<tbody>
				<xsl:apply-templates select="pm:activity"/>
			</tbody>
		</table>
	</xsl:template>
	
	<xsl:template match="pm:activity">
		<tr id="{@id}">
			<td align="right"><xsl:value-of select="@priority"/>&#160;</td>
			<xsl:variable name="uc" select="f:entity(@ref)"/>
			<td id="{substring-after(f:entity-id($uc), '#')}"><xsl:value-of select="$uc/pm:title"/></td>
			<td><xsl:value-of select="@resource"/></td>
			<td><xsl:value-of select="format-date(xs:date(@start), '[M]/[D]/[Y]')"/></td>
			<td><xsl:value-of select="format-date(xs:date(@end), '[M]/[D]/[Y]')"/></td>
		</tr>
	</xsl:template>
	
	<xsl:template match="pm:tests">
		<table border="1" cellspacing="0" cellpadding="2" class="overview">
			<v:style ref="{resolve-uri('styles.css')}"/>
			<thead>
				<tr>
					<th>Use Case</th>
					<th>Tester</th>
					<th>Time</th>
					<th>Result</th>
					<th>Comments</th>
				</tr>
			</thead>
			<xsl:apply-templates select="pm:test"/>
		</table>
	</xsl:template>
	
	<xsl:template match="pm:test">
		<tbody id="{@id}" style="white-space: nowrap">
			<tr>
				<xsl:variable name="uc" select="f:entity(@ref)"/>
				<td id="{substring-after(f:entity-id($uc), '#')}">
					<xsl:value-of select="$uc/pm:title"/>
				</td>
				<td><xsl:value-of select="@tester"/></td>
				<td><xsl:value-of select="format-dateTime(xs:dateTime(@time), '[M]/[D]/[Y]  [H01]:[m01]')"/></td>
				<td align="center"><xsl:value-of select="@result"/></td>
				<td><xsl:value-of select="."/></td>
			</tr>
		</tbody>
	</xsl:template>
	
</xsl:stylesheet>
